<?php
/**
 * Absctraction of a request
 */
class Request
{
    /**
     * If true - strip slashes from request vars
     * @var bool
     * @access private
     */
     var $_strip;

    /**
     * request args
     * @var array
     * @access private
     */
     var $_args = array();

    /** Constructor
     */
    function Request($args, $strip=false)
    {
        $this->_strip = $strip;
        $this->_args = $this->_trim($args);
    }

    /** Returns array of previous request message/errors and clean it.
     *
     */
    static function getMessages($url = false, $block='main')
    {
    	if (!$url){
    		$url = new Url();
    		if ($url->getParam("alias")){
    			$url->setFileName(dirname($url->getFileName())."/".$url->getParam("alias").".html");
				$url->setParam("alias");
    		}
    		$url = $url->getUrl();
            if (empty($_SESSION[$url]))
                $url = str_replace('/index.php', '', $url);
    	}
        $mes = array();
        if (!empty($_SESSION[$url][$block]['mes']) && is_array($_SESSION[$url][$block]['mes']))
            $mes = $_SESSION[$url][$block]['mes'];

        $err = array();
        if (!empty($_SESSION[$url][$block]['err']) && is_array($_SESSION[$url][$block]['err']))
            $err = $_SESSION[$url][$block]['err'];

       $_SESSION[$url][$block] = null;
       unset($_SESSION[$url][$block]);

       return array($mes, $err);
    }

    /** Forward request to new URL and call exit().
    * @param string $sUrl absolute URL to forward
    * @param mixed $mMes messags
    * @param mixed $mErr errors
    * @param string $block name of block
    * @return void
    */
    function forward($url, $mes=array(), $err=array(), $block='main')
    {
    	$url = new Url($url);
    	$url = $url->getUrl();
        if ($mes)
            $_SESSION[$url][$block]['mes'] = is_array($mes) ? $mes : array($mes);

        if ($err)
            $_SESSION[$url][$block]['err'] = is_array($err) ? $err : array($err);
        session_write_close(); // iis bug    
        header('Location: '.$url);
        exit;
    }
    
    /**
     * Sets request data
     *
     * @param string $name param name
     * @param mixed $val param value
     */
    function set($name, $val)
    {
        $this->_args[$name] = $val;
    }

    /**
    * Get action name.
    * @returns string
    */
    function getActionParam($def='')
    {
        $act = $this->get('act', $def);
        if (is_array($act)){
            $act1 = array_pop(array_keys($act));
            return array_pop(array_keys($act[$act1]));
        } else {
        	return false;
        }
    }

    /**
    * Get action name.
    * @returns string
    */
    function getAction($def='')
    {
        $act = $this->get('act', $def);
        if (is_array($act))
            $act = array_pop(array_keys($act));
        return $act;
    }
    
    /**
    * Retrieve from request specified argument.
    * @param string $name name of argument
    * @param string $def optional default value
    * @return string
    */
    function get($name, $def='')
    {
        // stupid ie
        if (empty($this->_args[$name]) && !empty($this->_args[$name."_x"]) && !empty($this->_args[$name."_y"]))
            return "1";
        return (empty($this->_args[$name]) ? $def : $this->_args[$name]);
    }

    function getInt($name, $def=0)
    {
        return intVal($this->get($name, $def));
    }

    function getFloat($name, $def=0.0)
    {
        return floatVal($this->get($name, $def));
    }

    function getArray($name, $def=array())
    {
        return (array)$this->get($name, $def);
    }
    
    /**
     * Gets phone number from request
     *
     * @param string $name prefix
     * @return string 'area-code-phone' or ''
     */
    function getPhone($name)
    {
        return $this->get($name);
    }

    /**
    * Retrieve from request all arguments.
    * @return array
    */
    function getArgs()
    {
        return $this->_args;
    }

    /** Trims spases and redundant slashes
    */
    function _trim($v)
    {
        if (is_array($v))
            return array_map(array($this, '_trim'), $v);
        if ($this->_strip)
            $v = stripslashes($v);
        return trim($v);
    }

    /** Returns array of previous request message/errors and clean it.
     *
     */
    function addErrors($errors = array(), $url = false, $block='main')
    {
    	if (!$url){
    		$url = new Url();
    		if ($url->getParam("alias")){
    			$url->setFileName(dirname($url->getFileName())."/".$url->getParam("alias").".html");
				$url->setParam("alias");
    		}
    		$url = $url->getUrl();
            if (empty($_SESSION[$url]))
                $url = str_replace('/index.php', '', $url);
    	}
		$_SESSION[$url][$block]['err'] = $errors;
    }

}

?>
